﻿using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;

using Model.Components;
using FrameWork;
using FrameWork.Components;
using FrameWork.WebControls;

namespace Model.Web.Module.Model.T_Web
{
    public partial class Manager : System.Web.UI.Page
    {
        public Int32 IDX = (Int32)Common.sink("IDX", MethodType.Get, 10, 0, DataType.Int);
        string CMD = (string)Common.sink("CMD", MethodType.Get, 10, 1, DataType.Str);
        protected void Page_Load(object sender, EventArgs e)
        {
            FrameWorkPermission.CheckPagePermission(CMD);
            if (!Page.IsPostBack)
            { 
                OnStart();
            }
        }
        
        /// <summary>
        /// 开始操作
        /// </summary>
        private void OnStart()
        {
            T_WebEntity ut = BusinessFacadeModel.T_WebDisp(IDX);
            Hidden1.Value = IDX.ToString();
            BindDep();
            OnStartData(ut);
            switch (CMD)
            { 
                 
                case "List":
                    TabOptionItem1.Tab_Name = HeadMenuWebControls1.HeadOPTxt = "查看网上投诉";
                    Hidden_Input();
                    CheckPermission();
                    AddEditButton();
                    break;
                case "Edit":
                    TabOptionItem1.Tab_Name = HeadMenuWebControls1.HeadOPTxt = "修改网上投诉";
                    Hidden_Disp();
                    AddDeleteButton();
                    break;
                case "Delete":
                    ut.DataTable_Action_ = DataTable_Action.Delete;
                    if (BusinessFacadeModel.T_WebInsertUpdateDelete(ut) > 0)
                    {
                        EventMessage.MessageBox(1, "删除成功", string.Format("删除ID:{0}成功!", IDX), Icon_Type.OK, Common.GetHomeBaseUrl("Default.aspx"));
                    }
                    else {
                        EventMessage.MessageBox(1, "删除失败", string.Format("删除ID:{0}失败!", IDX), Icon_Type.Error, Common.GetHomeBaseUrl("Default.aspx"));
                    }
                    break;
                default :
                    EventMessage.MessageBox(2, "不存在操作字符串!", "不存在操作字符串!", Icon_Type.Error, Common.GetHomeBaseUrl("Default.aspx"));
                    break;
            }
        }

        private void BindDep()
        {
            QueryParam qp = new QueryParam();
            int RecordCount = 0;
            qp.OrderType = 0;
            qp.Where = string.Format("Where G_ParentID =1 and G_Delete=0 ");
            ArrayList lst = BusinessFacade.sys_GroupList(qp, out RecordCount);
            foreach (sys_GroupTable var in lst)
            {
                 ListItem myListItem = new ListItem(var.G_CName, var.GroupID.ToString());
                 this.XCLDW_Input.Items.Add(myListItem);  
                 RecordCount++;
            }
        }
        /// <summary>
        /// 增加修改按钮
        /// </summary>
        private void AddEditButton()
        {
            HeadMenuButtonItem bi = new HeadMenuButtonItem();
            bi.ButtonPopedom = PopedomType.Edit;
            bi.ButtonName = "网上投诉";
            bi.ButtonUrl = string.Format("?CMD=Edit&IDX={0}", IDX);
            HeadMenuWebControls1.ButtonList.Add(bi);
        }


        /// <summary>
        /// 增加删除按钮
        /// </summary>
        private void AddDeleteButton()
        {
            HeadMenuButtonItem bi = new HeadMenuButtonItem();
            bi.ButtonPopedom = PopedomType.Delete;
            bi.ButtonName = "网上投诉";
            bi.ButtonUrlType = UrlType.JavaScript;
            bi.ButtonUrl = string.Format("DelData('?CMD=Delete&IDX={0}')", IDX);
            HeadMenuWebControls1.ButtonList.Add(bi);

            HeadMenuButtonItem bi1 = new HeadMenuButtonItem();
            bi1.ButtonPopedom = PopedomType.List;
            bi1.ButtonIcon = "back.gif";
            bi1.ButtonName = "返回";
            bi1.ButtonUrl = string.Format("?CMD=List&IDX={0}", IDX);
            HeadMenuWebControls1.ButtonList.Add(bi1);
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="ut"></param>
        private void OnStartData(T_WebEntity ut)
        {
            TID_Disp.Text = ut.TID.ToString();
            TName_Disp.Text = ut.TName.ToString();
            TNo_Disp.Text = ut.TNo.ToString();
            TPhone_Disp.Text = ut.TPhone.ToString();
            TGroup_Disp.Text = ut.TGroup.ToString();
            TBGName_Disp.Text = ut.TBGName.ToString();
            TBAddress_Disp.Text = ut.TBAddress.ToString();
            TTtle_Disp.Text = ut.TTtle.ToString();
            TContent_Disp.Text = ut.TContent.ToString(); 
            XType_Input.Value = XType_Disp.Text = ut.XType.ToString();
            XXian_Input.Text = XXian_Disp.Text = ut.XXian.ToString();
            XShi_Input.Text = XShi_Disp.Text = ut.XShi.ToString();
            XLingDao_Input.Text = XLingDao_Disp.Text = ut.XLingDao.ToString();
            XSumDay_Input.Text = XSumDay_Disp.Text = ut.XSumDay.ToString();
            XCLDW_Input.Text = ut.XCLDW.ToString();
            XCSDW_Disp.Text = XCSDW_Input.Text = ut.XCSDW.ToString();
            XCLDW_Disp.Text = BusinessFacade.get_table_fileds("sys_Group", "G_CName", "GroupID", ut.XCLDW.ToString());
            XXieBan_Input.Text = XXieBan_Disp.Text = ut.XXieBan.ToString();
            XIF_Disp.Text = ut.XIF.ToString();
        }

        private void CheckPermission()
        {
            string zhuangtai = BusinessFacade.get_table_fileds("T_Web","zhuangtai","ID", IDX.ToString());
            if (zhuangtai == "未处理")
            {
                if (FrameWorkPermission.CheckButtonPermission(PopedomType.Orderby))// 信访办
                {
                    bXinfang.Visible = true;
                    btoLingdao.Visible = true;

                    ShowInput();
                    Hidden_Disp();
                    XXian_Input.Visible = false;
                    XLingDao_Input.Visible = false;

                }
            }

            if (zhuangtai == "退回")
            {
                if (FrameWorkPermission.CheckButtonPermission(PopedomType.Orderby))// 信访办
                {
                    bXinfang.Visible = true;
                    btoLingdao.Visible = true;

                    ShowInput();
                    Hidden_Disp();
                    XXian_Input.Visible = false;
                    XLingDao_Input.Visible = false;

                } 
            } 


            if (zhuangtai == "领导审批")
            {
                 
                if (FrameWorkPermission.CheckButtonPermission(PopedomType.Print))//局长
                {
                    bLingdao.Visible = true; 

                    ShowInput();
                    Hidden_Disp();
                    XXian_Input.Visible = false;
                    XShi_Input.Visible = false;
                }
                
            }

            if (zhuangtai == "办理中")
            {
               
                if (FrameWorkPermission.CheckButtonPermission(PopedomType.A))//县市区
                {
                    bxian.Visible = true;
                    bback.Visible = true;

                    Hidden_Input();
                    XXian_Input.Visible = true;
                }
            }

            if (zhuangtai == "办结")
            {

               
            }
        }

        private void  ShowInput()
        {
             xtype.Visible = true;
            XType_Input.Visible = true;
            XXian_Input.Visible = true;
            XShi_Input.Visible = true;
            XLingDao_Input.Visible = true;
            XSumDay_Input.Visible = true;
            XCLDW_Input.Visible = true;
            XXieBan_Input.Visible = true;
            XCSDW_Input.Visible = true;
        }
        /// <summary>
        /// 隐藏输入框
        /// </summary>
        private void Hidden_Input()
        {
            xtype.Visible = false;
            XType_Input.Visible = false;
            XXian_Input.Visible = false;
            XShi_Input.Visible = false;
            XLingDao_Input.Visible = false;
            XSumDay_Input.Visible = false;
            XCLDW_Input.Visible = false;
            XCSDW_Input.Visible = false;
            XXieBan_Input.Visible = false;
        }

        /// <summary>
        /// 隐藏显示框
        /// </summary>
        private void Hidden_Disp()
        {

            XType_Disp.Visible = false;
            XXian_Disp.Visible = false;
            XShi_Disp.Visible = false;
            XLingDao_Disp.Visible = false;
            XSumDay_Disp.Visible = false;
            XCLDW_Disp.Visible = false;
            XCSDW_Disp.Visible = false;
            XXieBan_Disp.Visible = false;
            XIF_Disp.Visible = false;
        }
        /// <summary>
        /// 增加/修改事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            Int32 XSumDay_Value = (Int32)Common.sink(XSumDay_Input.UniqueID, MethodType.Post, 10, 0, DataType.Int);
            
                string XType_Value = (string)Common.sink(XType_Input.UniqueID, MethodType.Post, 500, 0, DataType.Str);
                string XXian_Value = (string)Common.sink(XXian_Input.UniqueID, MethodType.Post, 5000, 0, DataType.Str);
                string XShi_Value = (string)Common.sink(XShi_Input.UniqueID, MethodType.Post, 5000, 0, DataType.Str);
                string XLingDao_Value = (string)Common.sink(XLingDao_Input.UniqueID, MethodType.Post, 5000, 0, DataType.Str);

                 

                int XCLDW_Value = (int)Common.sink(XCLDW_Input.UniqueID, MethodType.Post, 10, 0, DataType.Int);
                string XXieBan_Value = (string)Common.sink(XXieBan_Input.UniqueID, MethodType.Post, 500, 0, DataType.Str);
               
                T_WebEntity ut = BusinessFacadeModel.T_WebDisp(IDX);

              
                ut.XType = XType_Value;
                ut.XXian = XXian_Value;
                ut.XShi = XShi_Value;
                ut.XLingDao = XLingDao_Value;
                ut.XSumDay = XSumDay_Value; 
                ut.XCLDW = XCLDW_Value;
                ut.XXieBan = XXieBan_Value; 

                if (CMD == "New")
                {
                    ut.DataTable_Action_ = DataTable_Action.Insert;
                }
                else if (CMD == "Edit")
                {
                    ut.DataTable_Action_ = DataTable_Action.Update;
                }
                else
                {
                    EventMessage.MessageBox(2, "不存在操作字符串!", "不存在操作字符串!", Icon_Type.Error, Common.GetHomeBaseUrl("Default.aspx"));
                }
                Int32 rInt = BusinessFacadeModel.T_WebInsertUpdateDelete(ut);
                if (rInt > 0)
                {
                    string OpTxt = string.Format("增加网上投诉成功!(ID:{0})", rInt);
                    if (ut.DataTable_Action_ == DataTable_Action.Update)
                        OpTxt = string.Format("修改网上投诉成功!(ID:{0})", IDX);
                    EventMessage.MessageBox(1, "操作成功", OpTxt, Icon_Type.OK, Common.GetHomeBaseUrl("Default.aspx"));
                }
                else if (rInt == -2)
                {
                    EventMessage.MessageBox(1, "操作失败", "操作失败,存在相同的键值(用户名/数据)!", Icon_Type.Alert, Common.GetHomeBaseUrl("Default.aspx"));
                }
                else
                {
                    EventMessage.MessageBox(1, "操作失败", string.Format("操作失败,返回值:{0}!", rInt), Icon_Type.Error, Common.GetHomeBaseUrl("Default.aspx"));
                }
        }

        protected void bXinfang_Click(object sender, EventArgs e)
        {
             Int32 XSumDay_Value = (Int32)Common.sink(XSumDay_Input.UniqueID, MethodType.Post, 10, 0, DataType.Int);
             if (XSumDay_Value == 0)
             {
                 EventMessage.MessageBox(2, "操作失败", string.Format("请输入要求办结上报时间!"), Icon_Type.Error, "history.back();", UrlType.JavaScript);
             }
             else
             { 
                 Update();
                 BusinessFacade.Update_Table_Fileds("T_Web", string.Format("XEndDate='{0}'", DateTime.Now.AddDays(Convert.ToDouble(XSumDay_Value))), string.Format("ID='{0}'", IDX.ToString()));
                 BusinessFacade.Update_Table_Fileds("T_Web", string.Format("zhuangtai='办理中'"), string.Format("ID='{0}'", IDX.ToString()));
                 BusinessFacade.Update_Table_Fileds("T_Web", string.Format("XCLDW='{0}'", XCLDW_Input.SelectedItem.Value), string.Format("ID='{0}'", IDX.ToString()));
                 EventMessage.MessageBox(1, "操作成功", "操作成功！", Icon_Type.OK, Common.GetHomeBaseUrl("Default.aspx"));
             }
        }
        private void Update()
        {
            Int32 XSumDay_Value = (Int32)Common.sink(XSumDay_Input.UniqueID, MethodType.Post, 10, 0, DataType.Int);
            string XType_Value = (string)Common.sink(XType_Input.UniqueID, MethodType.Post, 500, 0, DataType.Str);
            string XXian_Value = (string)Common.sink(XXian_Input.UniqueID, MethodType.Post, 5000, 0, DataType.Str);
            string XShi_Value = (string)Common.sink(XShi_Input.UniqueID, MethodType.Post, 5000, 0, DataType.Str);
            string XLingDao_Value = (string)Common.sink(XLingDao_Input.UniqueID, MethodType.Post, 5000, 0, DataType.Str); 
            DateTime? XEndDate_Value = DateTime.Now.AddDays(Convert.ToDouble(XSumDay_Value)); 
            int XCLDW_Value = (int)Common.sink(XCLDW_Input.UniqueID, MethodType.Post, 10, 0, DataType.Int);
            string XXieBan_Value = (string)Common.sink(XXieBan_Input.UniqueID, MethodType.Post, 500, 0, DataType.Str);
            string XCSDW = (string)Common.sink(XCSDW_Input.UniqueID, MethodType.Post, 500, 0, DataType.Str);
           BusinessFacade.Update_Table_Fileds("T_Web", string.Format("XSumDay='{0}'",XSumDay_Value), string.Format("ID='{0}'", IDX.ToString()));
           BusinessFacade.Update_Table_Fileds("T_Web", string.Format("XType='{0}'",XType_Value), string.Format("ID='{0}'", IDX.ToString()));
           BusinessFacade.Update_Table_Fileds("T_Web", string.Format("XXian='{0}'",XXian_Value), string.Format("ID='{0}'", IDX.ToString()));
           BusinessFacade.Update_Table_Fileds("T_Web", string.Format("XShi='{0}'",XShi_Value), string.Format("ID='{0}'", IDX.ToString()));
           BusinessFacade.Update_Table_Fileds("T_Web", string.Format("XLingDao='{0}'",XLingDao_Value), string.Format("ID='{0}'", IDX.ToString()));
           BusinessFacade.Update_Table_Fileds("T_Web", string.Format("XCSDW='{0}'", XCSDW), string.Format("ID='{0}'", IDX.ToString()));
           BusinessFacade.Update_Table_Fileds("T_Web", string.Format("XXieBan='{0}'",XXieBan_Value), string.Format("ID='{0}'", IDX.ToString())); 
        }

        protected void bLingdao_Click(object sender, EventArgs e)
        {
            Int32 XSumDay_Value = (Int32)Common.sink(XSumDay_Input.UniqueID, MethodType.Post, 10, 0, DataType.Int);
            if (XSumDay_Value == 0)
            {
                EventMessage.MessageBox(2, "操作失败", string.Format("请输入要求办结上报时间!"), Icon_Type.Error, "history.back();", UrlType.JavaScript);
            }
            else
            {
                Update();
                BusinessFacade.Update_Table_Fileds("T_Web", string.Format("XEndDate='{0}'", DateTime.Now.AddDays(Convert.ToDouble(XSumDay_Value))), string.Format("ID='{0}'", IDX.ToString()));
                BusinessFacade.Update_Table_Fileds("T_Web", string.Format("zhuangtai='办理中'"), string.Format("ID='{0}'", IDX.ToString()));
                BusinessFacade.Update_Table_Fileds("T_Web", string.Format("XCLDW='{0}'", XCLDW_Input.SelectedItem.Value), string.Format("ID='{0}'", IDX.ToString()));
                EventMessage.MessageBox(1, "操作成功", "操作成功！", Icon_Type.OK, Common.GetHomeBaseUrl("Default.aspx"));
            }
        }

        protected void btoLingdao_Click(object sender, EventArgs e)
        {
            Int32 XSumDay_Value = (Int32)Common.sink(XSumDay_Input.UniqueID, MethodType.Post, 10, 0, DataType.Int);
            if (XSumDay_Value == 0)
            {
                EventMessage.MessageBox(2, "操作失败", string.Format("请输入要求办结上报时间!"), Icon_Type.Error, "history.back();", UrlType.JavaScript);
            }
            else
            {
                Update();
                BusinessFacade.Update_Table_Fileds("T_Web", string.Format("XEndDate='{0}'", DateTime.Now.AddDays(Convert.ToDouble(XSumDay_Value))), string.Format("ID='{0}'", IDX.ToString()));
                BusinessFacade.Update_Table_Fileds("T_Web", string.Format("zhuangtai='办理中'"), string.Format("ID='{0}'", IDX.ToString()));
                BusinessFacade.Update_Table_Fileds("T_Web", string.Format("XCLDW='{0}'", XCLDW_Input.SelectedItem.Value), string.Format("ID='{0}'", IDX.ToString()));
                EventMessage.MessageBox(1, "操作成功", "操作成功！", Icon_Type.OK, Common.GetHomeBaseUrl("Default.aspx"));
            }
        }

        protected void bxian_Click(object sender, EventArgs e)
        {
            string XXian_Value = (string)Common.sink(XXian_Input.UniqueID, MethodType.Post, 5000, 0, DataType.Str);
            BusinessFacade.Update_Table_Fileds("T_Web", string.Format("XXian='{0}'", XXian_Value), string.Format("ID='{0}'", IDX.ToString()));
            BusinessFacade.Update_Table_Fileds("T_Web", string.Format("zhuangtai='办结'"), string.Format("ID='{0}'", IDX.ToString()));
            BusinessFacade.Update_Table_Fileds("T_Web", string.Format("XTime=getdate()"), string.Format("ID='{0}'", IDX.ToString()));
            BusinessFacade.Update_Table_Fileds("T_Web", string.Format("XIF='已办结'"), string.Format("ID='{0}'", IDX.ToString()));

            EventMessage.MessageBox(1, "操作成功", "操作成功！", Icon_Type.OK, Common.GetHomeBaseUrl("Default.aspx"));
        }

        protected void bback_Click(object sender, EventArgs e)
        {
            BusinessFacade.Update_Table_Fileds("T_Web", string.Format("zhuangtai='退回'"), string.Format("ID='{0}'", IDX.ToString()));

            EventMessage.MessageBox(1, "操作成功", "操作成功！", Icon_Type.OK, Common.GetHomeBaseUrl("Default.aspx"));
           
        }

        protected void bExp_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("../Exp.aspx?ID={0}",IDX.ToString()));
        }

        
    }
}
